using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormUIConfigEvent : Form
	{
		#region Initialization
		public FormUIConfigEvent(Repository.WorkflowEvent wfevent)
		{
			if ( wfevent == null ) throw new ArgumentNullException("wfevent");

			_wfevent = wfevent;

			InitializeComponent();

			txtEventName.Text = _wfevent.Name;
			txtEventOutput.Text = _wfevent.BusinessObjectName;

			displayMashupControlInfos();

			flowUIConfig.Size = groupUIConfig.ClientSize;
			tableUIConfig.Width = flowUIConfig.ClientSize.Width - tableUIConfig.Left * 2;
			groupSettingsBoxes.Width = flowUIConfig.ClientSize.Width - groupSettingsBoxes.Left * 2;
			groupSettingsButton.Width = flowUIConfig.ClientSize.Width - groupSettingsButton.Left * 2;
			groupSettingsCheckRadio.Width = flowUIConfig.ClientSize.Width - groupSettingsCheckRadio.Left * 2;
		}
		#endregion

		#region Public Members

		#region Properties
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Repository.WorkflowEvent _wfevent;
		#endregion

		#region Methods
		private void displayMashupControlInfos()
		{
			if ( _wfevent.MashupControl == null ) {
				this.SuspendLayout();
				groupSettingsBoxes.Visible = false;
				groupSettingsButton.Visible = false;
				groupSettingsCheckRadio.Visible = false;
				this.ResumeLayout(true);
				return;
			}

			txtControlName.Text = _wfevent.MashupControl.Name;
			txtControlType.Text = _wfevent.MashupControl.ControlType;

			this.SuspendLayout();
			groupSettingsBoxes.Visible = false;
			groupSettingsButton.Visible = false;
			groupSettingsCheckRadio.Visible = false;
			switch ( _wfevent.MashupControl.ControlType ) {
				case "System.Windows.Forms.Button" :
					groupSettingsButton.Visible = true;
					txtBtnText.Text = _wfevent.MashupControl.Text;
					cmbButtonEvent.SelectedItem = _wfevent.MashupControl.EventName;
					break;
				case "System.Windows.Forms.ComboBox" :
				case "System.Windows.Forms.ListBox" :
				case "System.Windows.Forms.CheckedListBox" :
					groupSettingsBoxes.Visible = true;
					cmbBoxEvent.SelectedItem = _wfevent.MashupControl.EventName;
					break;
				case "System.Windows.Forms.CheckBox" :
				case "System.Windows.Forms.RadioButton" :
					groupSettingsCheckRadio.Visible = true;
					txtCheckBoxText.Text = _wfevent.MashupControl.Text;
					cmbCheckBoxEvent.SelectedItem = _wfevent.MashupControl.EventName;
					break;
			}
			this.ResumeLayout(true);
		}
		#endregion

		#region Event Handlers
		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( txtControlName.Text != _wfevent.MashupControl.Name ) {
				_wfevent.Workflow.ExtendedProperties.MashupControls.Remove(_wfevent.MashupControl.Name);
				_wfevent.MashupControl.Name = txtControlName.Text;
				_wfevent.Workflow.ExtendedProperties.MashupControls.Add(_wfevent.MashupControl.Name, _wfevent.MashupControl);
			}
			if ( groupSettingsButton.Visible ) {
				_wfevent.MashupControl.Text = txtBtnText.Text;
				_wfevent.MashupControl.EventName = cmbButtonEvent.SelectedItem as string;
			} else if ( groupSettingsBoxes.Visible ) {
				_wfevent.MashupControl.EventName = cmbBoxEvent.SelectedItem as string;
			} else if ( groupSettingsCheckRadio.Visible ) {
				_wfevent.MashupControl.Text = txtCheckBoxText.Text;
				_wfevent.MashupControl.EventName = cmbCheckBoxEvent.SelectedItem as string;
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void btnCreateNewControl_Click(object sender, EventArgs e)
		{
			FormCreateMashupControl frm = new FormCreateMashupControl(_wfevent.Workflow, MashupUIConfigType.Event);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_wfevent.MashupControl = new OpenComposite.EII.Repository.Workflow.MashupControl();
				_wfevent.MashupControl.Name = frm.ControlName;
				_wfevent.MashupControl.ControlType = frm.ControlType.ToString();
				_wfevent.Workflow.ExtendedProperties.MashupControls.Add(frm.ControlName, _wfevent.MashupControl);
				displayMashupControlInfos();
			}
		}

		private void btnSelectControl_Click(object sender, EventArgs e)
		{
			FormSelectExistingMashupControl frm = new FormSelectExistingMashupControl(_wfevent.Workflow, MashupUIConfigType.Event);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_wfevent.MashupControl = frm.SelectedMashupControl;
				displayMashupControlInfos();
			}
		}

		#endregion

		#endregion

	}
}